package com.zephyr.vchcenter.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.zephyr.vchcenter.bean.Voucher;
import com.zephyr.vchcenter.dao.IVoucherDao;
@Deprecated
public class VoucherDaoImpl extends HibernateDaoSupport implements IVoucherDao {

	private static Logger log = Logger.getLogger(VoucherDaoImpl.class);
	@SuppressWarnings("unchecked")
	public List<Object> findByExample(final Object instance) {
		return (List) this.getHibernateTemplate().executeFind(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Example example = Example.create(instance)
								.excludeNone().excludeZeroes();
						
						List<Object> list = session.createCriteria(
								instance.getClass()).add(example).list();
						//System.out.println("list.size()"+list.size());
						return list;
					}
				});
	}

	public void saveInstance(final Object instance) {

		try{
			getHibernateTemplate().save(instance);
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Voucher> findByCondition(final Voucher voucher) {
		return (List) this.getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
//				Example example = Example.create(voucher).excludeNone().excludeZeroes();
				Criteria criteria = session.createCriteria(voucher.getClass());
				if (voucher.getChkstate() != null) {
					criteria.add(Restrictions.eq("chkstate", voucher.getChkstate()));
				}
				if (voucher.getIdstate()!=null) {
					criteria.add(Restrictions.eq("idstate", voucher.getIdstate()));
				}
//				criteria.add(example);
				List<Voucher> list = criteria.list();
				return list;
			}
		});
	}

	public void delete(Object instance) {
		try {
			getHibernateTemplate().delete(instance);
			log.debug("delete successful!!!");
		} catch (Exception e) {
			log.debug("delete error!!!");
		}
	}

	public Object findById(String id) {
		try {
			Object obj = getHibernateTemplate().get(Object.class, id);
			return obj;
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		}
		
	}

	public void save(Object instance) {
		try{
			getHibernateTemplate().save(instance);
			log.debug("successful!!!");
		}catch(Exception e){
			log.debug("save error!!!");
			e.printStackTrace();
		}
		
	}

	public void update(Object instance) {
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("save successful!!!");
		} catch (Exception e) {
			log.debug("saveOrUpdate error!!!");
			e.printStackTrace();
		}
		
	}

	public Object merge(Object instance) {
		try {
			Object obj = getHibernateTemplate().merge(instance);
			return obj;
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		}
		
	}

	
	
}
